package main

import (
	"fmt"
	pb "gRPC/serviceH/proto"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

type ClientTokenAuth struct {
}

func (c ClientTokenAuth) GetRequestMetadata(ctx context.Context, token ...string) (map[string]string, error) {
	//token密钥
	return map[string]string{
		"appId":  "CCC",
		"appKey": "JJJ",
	}, nil
}
func (c ClientTokenAuth) RequireTransportSecurity() bool {
	//不使用tsl加密
	return false
}

func main() {

	var opts []grpc.DialOption
	opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) //不使用tsl加密
	opts = append(opts, grpc.WithPerRPCCredentials(new(ClientTokenAuth)))

	//连接server端，此处禁用安全传输，没有加密和验证
	dial, err := grpc.Dial("127.0.0.1:8080", opts...)
	if err != nil {
		log.Fatal("连接错误：", err)
	}
	defer dial.Close()

	//建立连接
	client := pb.NewSayHelloClient(dial)

	//执行rpc的调用
	resp, _ := client.SayHello(context.Background(), &pb.HelloRequest{RequestName: "cjh"})
	fmt.Println("resp:", resp.GetResponseMsg())
}
